package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.Question;
import com.tanhua.domain.Settings;
import com.tanhua.domain.User;
import com.tanhua.domain.UserInfo;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.vo.PageResult;
import com.tanhua.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsServices {
    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    /**
     * 通用设置查询
     * @return
     */
    public SettingsVo settings() {
        SettingsVo settingsVo = new SettingsVo();
        //1.调用用户id
        User user = UserHolder.get();
        Long userId = user.getId();
        settingsVo.setId(userId);
        //2.获取用户手机号码
        String mobile = user.getMobile();
        settingsVo.setPhone(mobile);
        //3.获取用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        if(question != null){
            settingsVo.setStrangerQuestion(question.getTxt());
        }
        //4.获取用户的app通知开关数据
        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null){
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
        }
        return settingsVo;
    }

    public void saveQuestions(String content) {
        //获取用户id,根据id查询问题是否存在
        Long userId = UserHolder.getUserId();

        //获取用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        if (question == null){
            //如果问题不存在,保存
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }else {
            //问题存在,更新
            question.setTxt(content);
            questionApi.update(question);
        }


    }

    public void saveSettings(Map map) {
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean)  map.get("gonggaoNotification");
        //1.获取用于id
        Long userId = UserHolder.getUserId();
        Settings settings = settingsApi.findByUserId(userId);

        if(settings == null){
            settings = new Settings();
            settings.setId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.save(settings);
        }else{
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.update(settings);
        }

    }


    public PageResult blacklist(Integer page, Integer size) {
        Long userId = UserHolder.getUserId();

        IPage<UserInfo> iPage = blackListApi.findById(userId,page,size);

        PageResult pageResult = new PageResult(page,size, (int) iPage.getTotal(),iPage.getRecords());

        return pageResult;
    }

    public void delectBlack(Long blackId) {
        Long userId = UserHolder.getUserId();

        blackListApi.delectBlack(userId,blackId);
    }
}
